package com.junxonline.instant.dao.entity.vo.iobean;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Date;

/**
 * @author Virgo
 * @version 1.0
 * @data 2022/5/27 17:12
 * @usage 终端仅修改已存在的，不创建新的
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class ExcelTerminal extends BaseBean {
    public static final String SHEET_NAME = "终端信息";
    //导出表头
    private static final String TAG_ID = "序号";
    private static final String TAG_NAME = "终端名称";
    private static final String TAG_CODE = "终端编号(*)";
    private static final String TAG_VERSION = "软件版本";
    private static final String TAG_IP = "IP地址";
    private static final String TAG_MEMORY = "可用存储";
    private static final String TAG_HEARTBEAT = "最后心跳时间";
    private static final String TAG_STATE = "终端状态";
    private static final String TAG_ORGANIZATION = "组织机构(*)";//不变
    private static final String TAG_CITY = "所在地";
    private static final String TAG_GROUP = "所属分组";

    /**
     * 每列宽度
     */
    public static int[] getWidths(){
        return new int[]{10, 20, 25, 20, 20, 15, 25, 10, 25, 15, 15};
    }

    /**
     * 表头，中文名
     */
    public static String[] getHeaders() {
        return new String[]{TAG_ID, TAG_NAME, TAG_CODE, TAG_VERSION, TAG_IP, TAG_MEMORY, TAG_HEARTBEAT, TAG_STATE, TAG_ORGANIZATION, TAG_CITY, TAG_GROUP};
    }

    private int id;

    @JSONField(ordinal = 1)
    private String name;

    @JSONField(ordinal = 2)
    private String code;

    @JSONField(ordinal = 3)
    private String version;

    @JSONField(ordinal = 4)
    private String ip;

    @JSONField(ordinal = 5)
    private String memory;

    @JSONField(ordinal = 6)
    private Date heartbeat;

    @JSONField(ordinal = 7)
    private String state;

    @JSONField(ordinal = 8)
    private String organization;

    @JSONField(ordinal = 9)
    private String city;

    @JSONField(ordinal = 10)
    private String group;

    public void parseState(int state) {
        switch (state) {
            case 1:
                this.state = "停止";
                break;
            case 2:
                this.state = "播放";
                break;
            case 3:
                this.state = "多日离线";
                break;
            default:
                this.state = "离线";
                break;
        }
    }

    public void parseMemory(String mem){
        if(StrUtil.isEmpty(mem)){
            this.memory = "0 B";
        }

        long size = 0;
        try {
            size = Long.parseLong(mem);
        }catch (Exception e){
            e.printStackTrace();

        }
        if(size<=0){
            this.memory = "0 B";
        }else{
            this.memory = ExcelUtils.formatSize(size);
        }
    }

    /**
     * 导入，仅处理有用的数据
     *
     * @param key   中文，与导出头一致
     * @param value
     */
    public void setValue(String key, Object value) {
        switch (key) {
            case TAG_ID:
                setId(Integer.parseInt((String) value));
                break;
            case TAG_NAME:
                setName((String) value);
                break;
            case TAG_CODE:
                setCode((String) value);
                break;
            case TAG_CITY:
                setCity((String) value);
                break;
            case TAG_GROUP:
                setGroup((String) value);
                break;
        }
    }
}
